package com.hgx.db;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.junit.Test;

public class Demo {

	/**
	 * 测试query
	 */
	@Test
	public void dbTest() {

		String sql = "select id,username,sex,age,status from sys_user ";
		try {

			Connection conn = HC3P0ConnentionProvider.getConnection();
			PreparedStatement ps = conn.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				System.out.println("##############################");
				System.out.println("ID:" + rs.getString(1));
				System.out.println("name:" + rs.getString(2));
				System.out.println("sex:" + rs.getString(3));
				System.out.println("age:" + rs.getString(4));
				System.out.println("status:" + rs.getString(5));
				System.out.println("##############################");
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}

	/**
	 * 添加初始化数据
	 */
	@Test
	public void saveTestUser() {

		Connection conn = HC3P0ConnentionProvider.getConnection();
		try {
			conn.setAutoCommit(false);
			String sql;
			for (int i = 0; i < 5; i++) {
				sql = "insert into testuser(`id`,`username`,`sex`,`age`,`status`) values('" + i + "','张" + i + "','1','" + (10 + i) + "','0')";
				conn.prepareStatement(sql).executeUpdate();
			}
			conn.commit();
			System.out.println("数据添加完成!");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}// 设置事务

	}

}